﻿using CompanyRatings.PerfTest.Models.Query;
using CompanyRatings.SQL.PerfTest.ConsoleApp.Repositories;
using CompanyRatings.SQL.PerfTest.ConsoleApp.TestData;
using PerfTestLib.Spec;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CompanyRatings.PerfTest.ConsoleApp.TestData
{
    public class TestDataSource
    {
        List<WeightMatrix> WeightMatrices { get; set; }

        public TestDataSource()
        {
            LoadWeightMatrices();
        }

        private void LoadWeightMatrices()
        {
            this.WeightMatrices = new List<WeightMatrix>();

            // load weight matrices
            DSRSystemRepository repo = new DSRSystemRepository();
            this.WeightMatrices = repo.GetWeightMatrices();
        }


        public TestSpec<CrQuery> GetTestData(TestDataType testDataType)
        {
            TestSpec<CrQuery> spec = new TestSpec<CrQuery>(testDataType.ToString());


            #region XML :: Overload using full
            {
                List<CrQuery> matrixQueries = new List<CrQuery>();

                foreach (WeightMatrix matrix in this.WeightMatrices)
                {
                    string query_full_str = File.ReadAllText(@"TestData\query_full.xml");
                    CrQuery query_full = CrQuery.FromXml(query_full_str);
                    query_full.WeightMatrix.Id = matrix.Id;

                    matrixQueries.Add(query_full);
                }

                spec.Iterations.Add(
                                new TestSpecIteration<CrQuery>(
                                    id: string.Concat("test_full_p10_c", matrixQueries.Count, "_d", matrixQueries.Count),
                                    data: matrixQueries.ToArray(),
                                    count: matrixQueries.Count,
                                    maxParallelism: 10));
            }
            #endregion


            #region XML :: 2 ref. universes
            {
                string query_2refuniv_str = File.ReadAllText(@"TestData\query_2refuniv.xml");
                CrQuery query_2refuniv = CrQuery.FromXml(query_2refuniv_str);

                spec.Iterations.Add(
                            new TestSpecIteration<CrQuery>(
                                id: "test_2refuniv_p1_c3_d1",
                                data: new CrQuery[] { query_2refuniv },
                                count: 3,
                                maxParallelism: 1));

                spec.Iterations.Add(
                            new TestSpecIteration<CrQuery>(
                                id: "test_2refuniv_p2_c6_d1",
                                data: new CrQuery[] { query_2refuniv },
                                count: 6,
                                maxParallelism: 2));

                spec.Iterations.Add(
                            new TestSpecIteration<CrQuery>(
                                id: "test_2refuniv_p5_c15_d1",
                                data: new CrQuery[] { query_2refuniv },
                                count: 15,
                                maxParallelism: 5));

                spec.Iterations.Add(
                            new TestSpecIteration<CrQuery>(
                                id: "test_2refuniv_p10_c30_d1",
                                data: new CrQuery[] { query_2refuniv },
                                count: 30,
                                maxParallelism: 10));
            }
            #endregion


            #region XML :: full
            {
                string query_full_str = File.ReadAllText(@"TestData\query_full.xml");
                CrQuery query_full = CrQuery.FromXml(query_full_str);

                spec.Iterations.Add(
                            new TestSpecIteration<CrQuery>(
                                id: "test_full_p1_c3_d1",
                                data: new CrQuery[] { query_full },
                                count: 3,
                                maxParallelism: 1));

                spec.Iterations.Add(
                            new TestSpecIteration<CrQuery>(
                                id: "test_full_p2_c6_d1",
                                data: new CrQuery[] { query_full },
                                count: 6,
                                maxParallelism: 2));

                spec.Iterations.Add(
                            new TestSpecIteration<CrQuery>(
                                id: "test_full_p5_c15_d1",
                                data: new CrQuery[] { query_full },
                                count: 15,
                                maxParallelism: 5));

                spec.Iterations.Add(
                            new TestSpecIteration<CrQuery>(
                                id: "test_full_p10_c30_d1",
                                data: new CrQuery[] { query_full },
                                count: 30,
                                maxParallelism: 10));
            }
            #endregion


            return spec;
        }
    }
}
